package com.zds.support.util

import android.Manifest
import android.app.Activity
import android.os.Environment
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.example.zhouwei.library.CustomPopWindow
import com.github.dhaval2404.imagepicker.ImagePicker
import com.zds.support.R
import com.zds.support.base.SupportBaseActivity
import pub.devrel.easypermissions.EasyPermissions
import java.io.File
import java.util.*

/**
 * Author: zds
 * Date: 2022/11/9 10:33
 * Description:  https://github.com/Dhaval2404/ImagePicker
 * Version: v1.0
 */
class PopSelectPhotoUtil(val mActivity: Activity) {
    var popwindow: CustomPopWindow? = null
    companion object {
        val permissions = arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
        const val REQUEST_CODE_PICK_AVATER = 0X02
    }

    //选择弹出框
    fun select() {
        if (EasyPermissions.hasPermissions(mActivity, *permissions)) {
            val contentView: View = LayoutInflater.from(mActivity).inflate(R.layout.pop_select_phopo, null)
            contentView.findViewById<TextView>(R.id.btnGallery).setOnClickListener { startGallery() }
            contentView.findViewById<TextView>(R.id.btnCamera).setOnClickListener { startCamera() }
            contentView.findViewById<TextView>(R.id.btnCancel).setOnClickListener { popwindow!!.dissmiss() }

            popwindow = CustomPopWindow.PopupWindowBuilder(mActivity)
                .setView(contentView)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setAnimationStyle(0)
                .setBgDarkAlpha(0.7f) // 控制亮度
                .create()
                .showAtLocation(mActivity.window.decorView, Gravity.BOTTOM, 0, 0)
        } else {
            EasyPermissions.requestPermissions(mActivity, mActivity!!.getString(R.string.permissions_request_pick_avater), 1, *permissions)
        }
    }

    //打开相册
    fun startGallery() {
        if (EasyPermissions.hasPermissions(mActivity, *permissions)) {
            popwindow!!.dissmiss()
            ImagePicker.with(mActivity)
                .galleryOnly()
                //.crop()
                .compress(1024)
                .maxResultSize(620, 620)
                .saveDir(File(mActivity!!.getExternalFilesDir(Environment.DIRECTORY_DCIM), String.format(Locale.CHINA, "%s.jpg", System.currentTimeMillis())))
                .start(REQUEST_CODE_PICK_AVATER)
        } else {
            mActivity.let { EasyPermissions.requestPermissions(it, mActivity!!.getString(R.string.permissions_request_pick_avater), 1, *permissions) }
        }
    }

    //打开相机
    fun startCamera() {
        if (EasyPermissions.hasPermissions(mActivity!!, *permissions)) {
            popwindow!!.dissmiss()
            ImagePicker.with(mActivity!!)
                .cameraOnly()
                //.crop() 裁剪
                .compress(1024)
                .maxResultSize(620, 620)
                //.saveDir(File(mActivity!!.getExternalFilesDir(Environment.DIRECTORY_DCIM), String.format(Locale.CHINA, "%s.png", System.currentTimeMillis())))
                //.saveDir(File(StoreUtils.getSDPath(), String.format(Locale.CHINA, "%s.jpg", System.currentTimeMillis())))
                .saveDir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM))
                .start(REQUEST_CODE_PICK_AVATER)
        } else {
            mActivity.let { EasyPermissions.requestPermissions(it, mActivity!!.getString(R.string.permissions_request_pick_avater), 1, *permissions) }
        }
    }
}